package com.lan3g.Test.Model;


import com.lan3g.Test.pojo.User;
import com.lan3g.Test.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class LoginModel {
    public List<User> getUsers(String username) {
        List<User> users = new ArrayList<>();
        String sql = "";
        Connection conn = JDBCUtil.getConn();

        try {
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1, "%" + username + "%");
            ResultSet rs = statement.executeQuery();
            while (rs.next()) {
                User user = new User();
                //设置每一个用户的属性
                setUserParam(user, rs);
                users.add(user);//把这个用户加入到集合中
            }
            rs.close();
            statement.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return users;
    }

    public User login(String username, String password) {
        if (username == null || password == null) {
            return null;
        }
        try {
            User user = new User();
            Connection conn = JDBCUtil.getConn();
            String sql = "select * from testtable where username=? and pwd=?";
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1, username);
            statement.setString(2, password);
            ResultSet rs = statement.executeQuery();
            rs.next();
            //设置User属性
            setUserParam(user, rs);
            rs.close();
            statement.close();


            return user;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    private void setUserParam(User user, ResultSet rs) throws SQLException {
        String username = rs.getString("username");

        String nickname = rs.getString("nickname");
        int id = rs.getInt("uid");

        user.setUsername(username)
                .setNickName(nickname)
                .setPassword("");
    }


    //能否登录成功
    public boolean canLogin(String username, String pwd) {
        Connection conn = JDBCUtil.getConn();
        String sql = "select count(*) from testtable where username = ? and pwd=?";


        try {
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1, username);
            statement.setString(2, pwd);
            ResultSet rs = statement.executeQuery();
            boolean canLogin = false;
            if (rs.next()) {
                int count = rs.getInt(1);
                canLogin = count > 0;
            }
            statement.close();
            rs.close();


            return canLogin;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }}